package com.kingint.web.admin.base.controller;


import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kingint.common.unitedresult.K;

import com.kingint.web.admin.base.service.BasWarrantyItemsService;
import com.kingint.mbg.bas.entity.BasWarrantyItems;

import java.util.List;


/**
 * @描述：保修物品相关表控制类
 * @作者: wxh
 * @日期: 2020-12-21
 */
@RestController
@RequestMapping("/basWarrantyItems")
@Api(value = "BasWarrantyItemsController", description = "保修物品相关表")
@CrossOrigin
public class BasWarrantyItemsController{

    @Autowired
    public BasWarrantyItemsService basWarrantyItemsService;


    @ApiOperation(value = "添加保修物品相关表")
    @PostMapping("/create")
    //  @PreAuthorize("hasAuthority('basWarrantyItems:create')")
    public Object create(@Validated(Insert.class) @RequestBody BasWarrantyItems basWarrantyItems){
        int i = basWarrantyItemsService.insertWarrantyItemsInfo(basWarrantyItems);
        if(i>0){
            return K.ok().data(basWarrantyItems.getId());
        }else{
            return K.error();
        }
    }

    @ApiOperation(value = "更新保修物品相关表")
    @PutMapping("/update")
    // @PreAuthorize("hasAuthority('basWarrantyItems:update')")
    public Object update(@Validated(Update.class) @RequestBody BasWarrantyItems basWarrantyItems){
        int i = basWarrantyItemsService.updateWarrantyItems(basWarrantyItems);
        if(i>0){
            return K.ok().data(basWarrantyItems.getId());
        }else{
            return K.error();
        }
    }


    @ApiOperation(value = "删除保修物品相关表")
    @DeleteMapping("/delete/{id}")
    //  @PreAuthorize("hasAuthority('basWarrantyItems:delete')")
    public Object delete(@PathVariable("id") Integer id){
        int i = basWarrantyItemsService.deleteWarrantyItems(id);
        if(i>0){
            return K.ok();
        }else{
            return K.error().message("还有物品绑定到此分类");
        }
    }

    @ApiOperation(value = "根据保修物品相关表名称分页获取保修物品相关表列表")
    @GetMapping("/list")
    // @PreAuthorize("hasAuthority('basWarrantyItems:list')")
    public Object getList(BasWarrantyItems basWarrantyItems,
                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){

        return  basWarrantyItemsService.getWarrantyItemsList(basWarrantyItems, pageNum, pageSize);
    }

    @ApiOperation(value = "根据编号查询保修物品相关表信息")
    @GetMapping("/{id}")
    // @PreAuthorize("hasAuthority('basWarrantyItems:get')")
    public Object getItem(@PathVariable("id") Integer id){
        BasWarrantyItems basWarrantyItems=basWarrantyItemsService.getById(id);
        return K.ok().data(basWarrantyItems);
    }

    @ApiOperation(value = "获取所有的保修对象信息")
    @GetMapping("/getAllWarrantyInfo")
    public Object getAllWarrantyInfo(){
        List<BasWarrantyItems> basWarrantyItems=basWarrantyItemsService.getAllWarrantyInfo();
        return K.ok().data(basWarrantyItems);
    }
}